package com.qbw.core.base;

import android.app.Dialog;
import android.content.Context;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;

import com.qbw.app.R;
import com.qbw.log.XLog;

/**
 * Created by Bond on 2017/01/16 17:32
 * you can contact me at qbaowei@qq.com
 * 宽充满屏幕，高度自适应，居中
 */


public abstract class BaseDialog<B extends ViewDataBinding> extends Dialog {

    protected B mBinding;


    public BaseDialog(Context context) {
        super(context, R.style.BaseDialog);
    }

    public BaseDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        XLog.d(getClass().getName());
        onSetWindowFlag();
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()),
                                           getLayoutResId(),
                                           null,
                                           false);
        setContentView(mBinding.getRoot());
        onInit();
    }

    /**
     * 绑定布局
     */
    protected abstract int getLayoutResId();

    /**
     * 初始化
     */
    protected abstract void onInit();

    protected void onSetWindowFlag() {
        Window window = getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);//去掉系统添加的边距
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setWindowAnimations(getWindowAnimations());
        window.setAttributes(params);
        window.setGravity(Gravity.CENTER);
    }

    protected @StyleRes
    int getWindowAnimations() {
        return R.style.DialogAnimation;
    }

}
